package com.example.administrator.nurse.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseFragment;
import com.example.administrator.nurse.model.CommonViewHolder;
import com.example.administrator.nurse.model.RecyclerViewCommonAdapter;
import com.example.administrator.nurse.model.bean.PatientRoomBean;
import com.example.administrator.nurse.present.PatientInfoActivity;
import com.example.administrator.nurse.present.PatientRoomActivity;
import com.example.administrator.nurse.util.recyclerViewHelper;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 *  Created by Administrator on 2016/4/26.
 *  按病房
 *
 */

public class PatientRoomFragment extends BaseFragment implements PatientRoomActivity.OnDealWithDataListener {

    private boolean isFirstLoad = false;
    public RecyclerViewCommonAdapter adapter;
    private recyclerViewHelper recyclerViewHelper;
    private int[] flagList={R.mipmap.red_flag, R.mipmap.orange_flag, R.mipmap.yellow_flag, R.mipmap.green_flag};

    @Override
    protected void initView(final View rootView){

        initAdapter();
        recyclerViewHelper=new recyclerViewHelper(activity,(XRecyclerView)f(R.id.recyclerView))
                .withAdapter(adapter)
                .withLayoutManager(new GridLayoutManager(activity,3))
                .withPullRefrshOpen(true)
                .withRefresh(new recyclerViewHelper.refreshListener() {
                    @Override
                    public void refresh() {
                        PatientRoomActivity.PatientGetDataListener.onGetData(false);
                    }
                })
                .withLoadMore(new recyclerViewHelper.loadMoreListener() {
                    @Override
                    public void loadMore() {
                        PatientRoomActivity.PatientGetDataListener.onGetData(true);
                    }
                })
                .initRecyclerView();

        // 这里开始加载第一次数据
        if(!isFirstLoad){
            PatientRoomActivity.PatientGetDataListener.onGetData(false);
        }else{
            Log.d("zzzzz","不是第一次加载");
        }
    }

    @Override
    public int setLayoutId() {
        return R.layout.patient_room_fragment;
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {

    }
    //hhc 测试数据 实际要把for 注释掉
    private void initAdapter(){
        for (int i=0;i<20;i++){
            PatientRoomBean bean=new PatientRoomBean();
            bean.setRoomnum("5");
            bean.setBednum("7");
            bean.setDate("2016-3-23");
            bean.setTime("12:11:23");
            bean.setState(PatientRoomBean.SPECIFY);
            PatientRoomActivity.list.add(bean);
        }

        adapter=new RecyclerViewCommonAdapter(getActivity(),PatientRoomActivity.list) {

            @Override
            protected int getLayoutId() {
                return R.layout.patient_room_item;
            }

            @Override
            protected void bindView(CommonViewHolder holder, final int position) {
                RelativeLayout beanfather;
                TextView bed_num,room_num,date,time;
                ImageView flag;
                PatientRoomBean bean = PatientRoomActivity.list.get(position);

                room_num = holder.getTextView (R.id.room_num);
                bed_num  = holder.getTextView (R.id.bed_num );
                //date     = holder.getTextView (R.id.date    );
                time     = holder.getTextView (R.id.time    );
                flag     = holder.getImageView(R.id.flag    );

                room_num.setText(bean.getRoomnum());
                bed_num.setText(bean.getBednum());
                //date.setText(bean.getDate());
                time.setText(bean.getTime());
                flag.setImageResource(flagList[bean.getLevel()]);

                beanfather=holder.getRelativeLayout(R.id.bean_father);
                beanfather.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(null,position);
                    }
                });
            }
        };

        adapter.setOnItemClickListener(new RecyclerViewCommonAdapter.OnItemClickListener(){

            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(),PatientInfoActivity.class);
                intent.putExtra("data",PatientRoomActivity.list.get(position));
                Log.d("zzzzz","====="+PatientRoomActivity.list.get(position).getDoc());
                startActivityForResult(intent, PatientRoomActivity.OnSaveCallBack);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onSuccessDealWith(int requestCode, Object object, String response) {
        List<String> l = (List<String>) object;
        Log.d("zzzzz", l.get(0));
        try {
            PatientRoomActivity.SearchInput.type.setText(l.get(0));
            PatientRoomActivity.SearchInput.sort.setText(l.get(1));
            PatientRoomActivity.dealDataJson(response, false);
            switch (requestCode) {
                case PatientRoomActivity.GetDataNormal:
                    recyclerViewHelper.getRecyclerView().refreshComplete();
                    if (response.trim().equals("0")) {
                        t("暂无记录!");
                        PatientRoomActivity.list.clear();
                        adapter.notifyDataSetChanged();
                        //userTestData(); /** 数据库没数据，使用测试数据，日后可以删除 */
                    } else {
                        t(response);
                        if (!isFirstLoad) {
                            isFirstLoad = true;
                        }
                        adapter.notifyDataSetChanged();
                    }
                    break;
                case PatientRoomActivity.GetDataMore:
                    recyclerViewHelper.getRecyclerView().loadMoreComplete();
                    PatientRoomActivity.dealDataJson(response, true);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }catch (Exception ignored){}
    }

    @Override
    public void onFailureDealWith(int requestCode, Object object, String response) {

    }
}
